#include "stdafx.h"
#include "RecentChangesChannel.h"

CRecentChangesChannel::CRecentChangesChannel(QString user, QString channel):
m_user(user),m_channel(channel),
m_changeRegex(":rc!~rc@127\\.0\\.0\\.1 PRIVMSG " + channel + " :\\000314\\[\\[\\000307([^\\0003]+)\\000314\\]\\]"
			  "\\00034 [MB]*\\000310 \\000302http://.+index\\.php\\?diff=(\\d+).+\\000303([^\\0003]+)\\0003 \\00035"
			  "\\*\\0003 \\(\\0002?([+\\-]\\d+)\\0002?\\) \\000310.*\\0003")
			  /* Capture groups:
					1: Page title
					2: Diff
					3: User
					4: Bytes changed
			  */
{
}

CRecentChangesChannel::~CRecentChangesChannel(void)
{
}

QHostAddress CRecentChangesChannel::GetHost() const
{
	QHostInfo info = QHostInfo::fromName("irc.wikimedia.org");
	// TODO: Make this choose more reasonably
	return info.addresses().first();

}

unsigned short CRecentChangesChannel::GetPort() const
{
	return 6667;
}

void CRecentChangesChannel::ProcessIncomingCommand( QString command )
{
	if (m_changeRegex.indexIn(command) >= 0)
	{
		QString page = m_changeRegex.cap(1);
		QString user = m_changeRegex.cap(3);
		QString diff = m_changeRegex.cap(2);
		QString count = m_changeRegex.cap(4);

		emit RecentChange(page, user, diff.toUInt(), count.toInt());
		
	}
}

QString CRecentChangesChannel::GetChannel() const
{
	return m_channel;
}

QString CRecentChangesChannel::GetUser() const
{
	return m_user;
}